﻿using BaseCore;
using BaseCore.RemoteServiceInterface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Tools.Base;

namespace RemoteClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string strFileName = Process.GetCurrentProcess().MainModule.FileName + ".config";
            System.Runtime.Remoting.RemotingConfiguration.Configure(strFileName, false);

            RunProgram_Input(args);
        }


        public static void RunProgram_Input(string[] args)
        {
            Thread thread = new Thread(ThreadFunc_ConsoleInput)
            {
                IsBackground = true
            };

            thread.Start();
            while (!NeedExitServer || !CanExitServer)
            {
                SendMsgToRemoteServer();
                Thread.Sleep(1000);
            }
            thread.Abort();
        }

        static ICalculaterService _service = null;
        static int _nIndex = 0;

        static private void SendMsgToRemoteServer()
        {
            MyConsole.WriteLineEx(string.Format("发送消息..."), ConsoleColor.Green);

            if (_service == null)
            {
                string strUri = ConfigurationManager.AppSettings.Get("CalculaterUri");
                _service = (ICalculaterService)Activator.GetObject(typeof(ICalculaterService), strUri);
            }

            if (_service == null)
                return;

            CalculateInfo info = new CalculateInfo()
            {
                p1 = _nIndex++,
                p2 = _nIndex++,
            };
            CalculateRes ret = _service.Calculate(info);

            MyConsole.WriteLineEx(string.Format("收到消息 Index={0} value={1}", ret.nIndex, ret.nValue), ConsoleColor.Blue);
        }

        /// <summary>
        /// 控制台指令的线程函数
        /// </summary>
        public static void ThreadFunc_ConsoleInput()
        {
            Console.Title = "RemoteClient";

            while (!NeedExitServer)
            {
                string cmd = Console.ReadLine();
                if (!string.IsNullOrEmpty(cmd))
                {
                    if (0 == cmd.CompareTo("exit"))
                    {
                        MyConsole.WriteLineEx("确认退出吗(输入 y 将立即退出)？");
                        cmd = Console.ReadLine();
                        if (0 == cmd.CompareTo("y"))
                        {
                            break;
                        }
                    }
                }
            }

            ExitServer();
        }

        private static int _NeedExitServer = 0;
        private static int _CanExitServer = 0;
        public static bool NeedExitServer { get { return _NeedExitServer == 1; } }
        public static bool CanExitServer { get { return _CanExitServer == 1; } }

        private static void ExitServer()
        {
            int nOriginalValue = Interlocked.Exchange(ref _NeedExitServer, 1);
            if (nOriginalValue == 1)
                return;

            Interlocked.Exchange(ref _CanExitServer, 1);
        }
    }
}
